﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using UPMS.Core.Domain;

namespace UPMS.Web.Controllers
{
    public class AccountController : BaseController
    {
        // GET: /Account/
        public ActionResult Index()
        {
            return View(accountDao.Select());
        }

        // GET: /Account/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Account account = accountDao.Select(id.Value);
            if (account == null)
            {
                return HttpNotFound();
            }
            return View(account);
        }

        // GET: /Account/Create
        public ActionResult Create()
        {
            ViewBag.ApplicationId = new SelectList(appDao.Select(), "Id", "Name");
            return View();
        }

        // POST: /Account/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Password,ApplicationId,Status,Description,Created,CreatedBy,Modified,ModifiedBy")] Account account)
        {
            if (ModelState.IsValid)
            {
                account.Id = Guid.NewGuid();
                accountDao.Insert(account);
                return RedirectToAction("Index");
            }

            ViewBag.ApplicationId = new SelectList(appDao.Select(), "Id", "Name", account.ApplicationId);
            return View(account);
        }

        // GET: /Account/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Account account = accountDao.Select(id.Value);
            if (account == null)
            {
                return HttpNotFound();
            }
            ViewBag.ApplicationId = new SelectList(appDao.Select(), "Id", "Name", account.ApplicationId);
            return View(account);
        }

        // POST: /Account/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Password,ApplicationId,Status,Description,Created,CreatedBy,Modified,ModifiedBy")] Account account)
        {
            if (ModelState.IsValid)
            {
                accountDao.Update(account);
                return RedirectToAction("Index");
            }
            ViewBag.ApplicationId = new SelectList(appDao.Select(), "Id", "Name", account.ApplicationId);
            return View(account);
        }

        // GET: /Account/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Account account = accountDao.Select(id.Value);
            if (account == null)
            {
                return HttpNotFound();
            }
            return View(account);
        }

        // POST: /Account/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            accountDao.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
